package com.zhanlv.socketnet

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import org.json.JSONObject
import java.net.Socket

class RemoteKotlinActivity : AppCompatActivity() {

    companion object{
        const val TAG = "wooran"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_remote_kotlin)

        findViewById<Button>(R.id.btn_test).setOnClickListener {
            Thread(Runnable {
                var jsonObject = JSONObject()
                jsonObject.put("androidId", "androidid_123456")

                var jsonStr = jsonObject.toString();
                var payload = "${jsonStr.length}\n$jsonStr"

                var socket = Socket("139.196.17.31", 6667)

                Thread.sleep(1000)
                Log.e(TAG, "is connected: ${socket.isConnected}")

                socket.getOutputStream().write(payload.toByteArray(Charsets.UTF_8));

            }).start();
        }
    }
}